package org.millioncall.yueyoga.admin.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.Place;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.PlaceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PlaceAction extends BaseAction {
	private PlaceService placeService;

	public PlaceService getPlaceService() {
		return placeService;
	}

	public void setPlaceService(PlaceService placeService) {
		this.placeService = placeService;
	}

	// 查询场地信息
	@RequestMapping(value = "/findPlaceList.jhtml")
	public String getPlaseByPage(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		int start = 0;
		int pageSize = 20;

		String strStart = request.getParameter("start");
		if (strStart != null) {
			start = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if (strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		String name=request.getParameter("name");
		if (StringUtils.isNotBlank(name)) {
			map.put("name", "%"+name+"%");
			context.put("name", name);
		}
		String type=request.getParameter("type");
		if (StringUtils.isNotBlank(type)) {
			map.put("type", type);
			context.put("type", type);
		}
		String status=request.getParameter("status");
		if (StringUtils.isNotBlank(status)) {
			map.put("status", Boolean.parseBoolean(status));
			context.put("status", status);
		}
		
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		PaginatedList<Place> p = placeService.getPlaseInPage(start, pageSize,map);
		context.put("placeList", p);
		context.put("totalspeakers", p.getPager().getCountOfTotalResults());
		return "place_search";
	}

	// 添加场地信息
	@RequestMapping(value = "/toAddPlace.jhtml")
	public String toAddPlace(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		return "place_add";
	}

	// 添加场地信息
	@RequestMapping(value = "/addPlace.jhtml")
	public String addplace(Map<String, Object> context,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {

		super.initFrontHeader(request, model);

		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			context.put("msg", "场地信息姓名不可为空");
			return "place_add";
		}
		if (name.length() > 15) {
			context.put("msg", "场地信息姓名长度应介于2-15之间");
			return "place_add";
		}
		
		Place place = new Place();
		place.setName(name);
		place.setStatus(0);
		place.setType(Integer.parseInt(request.getParameter("type")));
		Date time=new Date();
		place.setCreatetime(time);
		User user = this.getUser(request);
		place.setOrganizationId(user.getOrganizationId());
		if (placeService.insert(place)) {
			return "redirect:findPlaceList.jhtml";
		} else {
			context.put("msg", "添加场地信息失败，请稍候重试或联系管理员");
			return "place_add";
		}
	}

	// 添加场地信息
	@RequestMapping(value = "/toEditPlace.jhtml")
	public String toEditPlace(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		Integer id = Integer.parseInt(request.getParameter("id"));
		Place place = placeService.findPlaceById(id);
		context.put("place", place);
		return "place_edit";
	}

	// 编辑场地信息
	@RequestMapping(value = "/editPlace.jhtml")
	public String editplace(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		Integer id = Integer.parseInt(request.getParameter("placeId"));
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			context.put("msg", "场地信息性名不可为空");
			return "place_edit";
		}
		if (name.length() > 15) {
			context.put("msg", "场地信息姓名长度应介于2-15之间");
			return "place_edit";
		}
		Place place = placeService.findPlaceById(id);
		place.setName(name);
		place.setStatus(Integer.parseInt(request.getParameter("status")));
		place.setType(Integer.parseInt(request.getParameter("type")));
		if (placeService.updatePlace(place)) {
			context.clear();
			return "redirect:findPlaceList.jhtml";
		} else {
			context.put("msg", "更新场地信息失败，请稍候重试或联系管理员");
			return "place_edit";
		}
	}

	// 删除场地信息
	@RequestMapping(value = "/deletePlace.jhtml")
	public void deleteplace(HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除场地信息的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids = new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					deids[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				placeService.deletePlaseByIds(deids);
				msg = "true";
			} catch (RuntimeException re) {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

}
